﻿using System;
using System.Collections.Generic;
using System.Linq;
using AutoStopData.Domains;

namespace AutoStopApp.Core
{
    public class WsaaManager
    {
        public static readonly List<AccessItem> Data;

        static WsaaManager()
        {
            var itemDefault = new MenuItem { Text = null, Url = "/Default.aspx" };
            var itemDefaultInner = new MenuItem { Text = "Úvod", Url = "/Pages/Default.aspx" };
            var itemTripSearch = new MenuItem { Text = "Vyhľadávanie", Url = "/Pages/TripSearch.aspx" };
            var itemTripList = new MenuItem { Text = "Zoznam ciest", Url = "/Pages/TripList.aspx" };
            var itemUserList = new MenuItem { Text = "Užívatelia", Url = "/Pages/UserList.aspx" };
            var itemUserRegister = new MenuItem { Text = "Registrácia", Url = "/Pages/UserRegister.aspx" };
            var itemPasswordChange = new MenuItem { Text = null, Url = "/Pages/PasswordChange.aspx" };
            var itemActivity = new MenuItem { Text = null, Url = "/Pages/Activity.aspx" };

            Data = new List<AccessItem>();

            Data.Add(new AccessItem(UserTypes.Name.Administrator, WsaaResource.DefaultPage, itemDefault));
            Data.Add(new AccessItem(UserTypes.Name.Administrator, WsaaResource.DefaultInnerPage, itemDefaultInner));
            Data.Add(new AccessItem(UserTypes.Name.Administrator, WsaaResource.TripSearchPage, itemTripSearch));
            Data.Add(new AccessItem(UserTypes.Name.Administrator, WsaaResource.TripListPage, itemTripList));
            Data.Add(new AccessItem(UserTypes.Name.Administrator, WsaaResource.UserListPage, itemUserList));
            Data.Add(new AccessItem(UserTypes.Name.Administrator, WsaaResource.PasswordChangePage, itemPasswordChange));
            Data.Add(new AccessItem(UserTypes.Name.Administrator, WsaaResource.ActivitypPage, itemActivity));
            
            Data.Add(new AccessItem(UserTypes.Name.Driver, WsaaResource.DefaultPage, itemDefault));
            Data.Add(new AccessItem(UserTypes.Name.Driver, WsaaResource.DefaultInnerPage, itemDefaultInner));
            Data.Add(new AccessItem(UserTypes.Name.Driver, WsaaResource.TripSearchPage, itemTripSearch));
            Data.Add(new AccessItem(UserTypes.Name.Driver, WsaaResource.TripListPage, itemTripList));
            Data.Add(new AccessItem(UserTypes.Name.Driver, WsaaResource.PasswordChangePage, itemPasswordChange));
            Data.Add(new AccessItem(UserTypes.Name.Driver, WsaaResource.ActivitypPage, itemActivity));
            
            Data.Add(new AccessItem(UserTypes.Name.User, WsaaResource.DefaultPage, itemDefault));
            Data.Add(new AccessItem(UserTypes.Name.User, WsaaResource.DefaultInnerPage, itemDefaultInner));
            Data.Add(new AccessItem(UserTypes.Name.User, WsaaResource.TripSearchPage, itemTripSearch));
            Data.Add(new AccessItem(UserTypes.Name.User, WsaaResource.PasswordChangePage, itemPasswordChange));
            Data.Add(new AccessItem(UserTypes.Name.User, WsaaResource.ActivitypPage, itemActivity));

            Data.Add(new AccessItem(UserTypes.Name.None, WsaaResource.DefaultPage, itemDefault));
            Data.Add(new AccessItem(UserTypes.Name.None, WsaaResource.DefaultInnerPage, itemDefaultInner));
            Data.Add(new AccessItem(UserTypes.Name.None, WsaaResource.TripSearchPage, itemTripSearch));
            Data.Add(new AccessItem(UserTypes.Name.None, WsaaResource.UserRegisterPage, itemUserRegister));
            Data.Add(new AccessItem(UserTypes.Name.None, WsaaResource.ActivitypPage, itemActivity));
        }

        public static bool IsAuthorized(UserTypes.Name userTypeName, WsaaResource resource)
        {
            bool retval = Data.Any(o => o.UserTypeName == userTypeName && o.Resource == resource);
            return retval;
        }

        public static bool IsAuthorized(string userTypeCode, WsaaResource resource)
        {
            UserTypes.Name userTypeName = UserTypes.GetName(userTypeCode);
            bool retval = Data.Any(o => o.UserTypeName == userTypeName && o.Resource == resource);
            return retval;
        }

        public class AccessItem
        {
            public UserTypes.Name UserTypeName { get; set; }
            public WsaaResource Resource { get; set; }
            public MenuItem MenuItem { get; set; }

            public AccessItem(UserTypes.Name userTypeName, WsaaResource resource, MenuItem menu)
            {
                UserTypeName = userTypeName;
                Resource = resource;
                MenuItem = menu;
            }
        }

        public enum WsaaResource
        {
            None,
            DefaultPage,
            TripSearchPage,
            TripListPage,
            UserListPage,
            UserRatingPage,
            UserRegisterPage,
            PasswordChangePage,
            DefaultInnerPage,
            ActivitypPage
        }

        public class MenuItem
        {
            public string Text { get; set; }
            public string Url { get; set; }
        }
    }
}